package com.llamarun;

import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Potato extends Projectible{
	private int i =0;
	private boolean improved= false;
	private boolean explosive = false;
	private double angle = 180;
	private final int numberOfExplosions = 7;
	private Random randomGenerator;
	private Bitmap smallPotato;
	
	public Potato (GameView view)
	{
    	this.randomGenerator = new Random();
		this.view = view;
		this.smallPotato = BitmapFactory.decodeResource(view.getResources(), R.drawable.potato_small);
		this.setBitmap(BitmapFactory.decodeResource(view.getResources(), R.drawable.potato2));
	}
	
	public void setExplosive () {
		this.explosive = true;
	}
	
	public void setAngle (Double angle) {
	  this.angle = angle;	
	}
	
	public double getNoise () {
		return (randomGenerator.nextDouble()*50)-25;
	}
	
	public void fly()
	{
		if(!explosive) {
		  if(improved) {
            if (i<12){
        	    this.setX(this.getX()+speed);
    		    this.setY(this.getY()+i);
    		    i++;
    		  
            }
            else if (i ==12){
            	for (int j=1; j<numberOfExplosions; j++) {
        	      Potato p = new Potato(this.view);
        	      p.setX(this.getX());
        	      p.setY(this.getY());
        	      p.setExplosive();
        	      p.setAngle(((double)j/numberOfExplosions)*360+getNoise());
        	      p.flying = true;
        	      p.setBitmap(smallPotato);
    		      this.view.addGameObject(p);
            	}
            	this.setBitmap(smallPotato);
                i++;
            }
            else if (i>12){
            	this.setX(this.getX()+(int)(1*i*10));
    		    this.setY(this.getY());
    		    i++;
            }
	      }
		  else {
	        this.setX(this.getX()+speed);
		    this.setY(this.getY()+i);
		    i++;
		  }
		}
		else {
			this.setX(this.getX()+(int)(Math.cos(angle)*i*10));
		    this.setY(this.getY()+(int)(Math.sin(angle)*i*10));
		    i++;
		}
	}
	
	public void improve()
	{
		improved = true;
		//if (improved = false) {
		  //speed=speed*2;
		  //improved = true;
		//}
	}
	
	public void unimprove() {
	  improved = false;	
	}
	
}
